package com.example.system.config;

import com.example.system.interceptor.AllowCrossDomainInterceptor;
import com.example.system.interceptor.AuthorizationInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Resource
    private AllowCrossDomainInterceptor allowCrossDomainInterceptor;
    @Resource
    private AuthorizationInterceptor authorizationInterceptor;

    @Value("${uploadPath}")
    private String uploadPath;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(allowCrossDomainInterceptor).addPathPatterns("/system/api/**");
        registry.addInterceptor(authorizationInterceptor)
                .addPathPatterns("/system/api/**")
                .excludePathPatterns(
                        "/system/api/captcha",
                        "/system/api/login");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/upload/**").addResourceLocations("file:"+uploadPath);
    }
}
